var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'report_avg_billing.html', controller: ListExpMstCtrl}).      
      when('/cancel/', {templateUrl: 'report_avg_billing.html', controller: ListExpMstCtrl}). 
      otherwise({redirectTo: '/'});
}]);

function ListExpMstCtrl($scope, $http,$window,$location,$filter)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 25;
	$scope.Math = window.Math;
	$scope.ed = false;
	$scope.yr_arr = [];	
	
	var d = new Date();
	$scope.search1 = d.getFullYear();
	$scope.search2 = d.getMonth()+1;
	
	$scope.file = 'report_avg_billing_data.php?year='+$scope.search1+'&month='+$scope.search2+'&id=2';
    $http.get($scope.file).success(function(data) {	
		$scope.mismast = data;
	 });
	 
	 $http.get('report_avg_billing_data.php?id=1').success(function(data) {	
		for(var i=0; i<data.length;i++){				
			$scope.yr_arr[i] = data[i];				
		}				
		$scope.yr_arr.sort();
		$scope.yr_arr.reverse();		
    });
	  
	$scope.cal_grandtot=function()
	{
		var total = 0;
        angular.forEach($scope.mismast, function(tran) {
         	total +=  tran.totalamt;			
        })
        return total;
	}
	
	$scope.changemon=function()
	{		
		$scope.file = 'report_avg_billing_data.php?year='+$scope.search1+'&month='+$scope.search2+'&id=2';
		$http.get($scope.file).success(function(data) {				
			$scope.mismast = data;	
		});
	}
	
	$scope.$watch('search2', function (newVal, oldVal) {
		if (oldVal == newVal) return;		
		$scope.changemon();		
	}, true);
	$scope.$watch('search1', function (newVal, oldVal) {
		if (oldVal == newVal) return;		
		$scope.changemon();		
	}, true);
	
	
	//Function to Clear 
    $scope.cancel= function()
	{
		$scope.activePath = null;
		$scope.activePath = $location.path('report_avg_billing.php');
	};
      
 }
    
//Custom Filter Function to sort the record.
app.filter('startingFrom', function() 
{
    return function(input, start) 
    {
        start = +start; //parse to int
        return input.slice(start);
    };
});
	
	